package com.ymatou.iapi.csim.testcase;

import com.ymatou.iapi.csim.parameter.GetUnReadCountBean;
import com.ymatou.iapi.csim.parameter.SendMessageBean;
import com.ymatou.iapi.csim.parameter.SendMessageCSBean;
import com.ymatou.iapi.csim.service.GetUnReadCountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

import org.junit.*;
import org.junit.experimental.categories.Category;
/**
*获取用户与洋管家消息未读数
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUnReadCount{
	private static GetUnReadCountBean bean;
	private static GetUnReadCountCall call;
	private CsimCallService service=new CsimCallService();
	private Integer csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private static Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private Integer sellerId=Integer.parseInt(EnvSetup.getData("autotest_seller1").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	private static ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户与洋管家消息未读数");
	}

	@Before
	public void caseUp() {
		bean=new GetUnReadCountBean();
		call=new GetUnReadCountCall();
		Logger.comment("删除旧数据");
		csSessionWapper.deleteByUserId(MapUtil.hashMap("UserId",userId));
		onlinecsWapper.deleteBy(MapUtil.hashMap("CSId",csId));
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUnReadCount_001() {
		Logger.start(true,"获取用户与洋管家消息未读数-买家");
		try {
			//客服上线
			service.getOnlineCS(csId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();		
			int count1=Integer.parseInt(call.getResult().get("Count").toString());
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId());
			sendMessageCSBean.setCsId(csId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			call.callService();	
			int count2=Integer.parseInt(call.getResult().get("Count").toString());
			
			Logger.verifyEquals("0", call.getResult().get("ErrorCode").toString(), "验证errorCode");
			Logger.verifyEquals(true, call.getResult().get("Success"), "验证Success");
			Logger.verifyEquals("200", call.getStatus(), "验证Status");
			Logger.verifyEquals(1, count2-count1, "验证未读总数加1");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUnReadCount_002() {
		Logger.start(true,"获取用户与洋管家消息未读数-买手");
		try {
			//客服上线
			service.getOnlineCS(sellerId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(sellerId,userId);
			
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();		
			int count1=Integer.parseInt(call.getResult().get("Count").toString());
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId());
			sendMessageCSBean.setCsId(sellerId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			call.callService();	
			int count2=Integer.parseInt(call.getResult().get("Count").toString());
			
			Logger.verifyEquals("0", call.getResult().get("ErrorCode").toString(), "验证errorCode");
			Logger.verifyEquals(true, call.getResult().get("Success"), "验证Success");
			Logger.verifyEquals("200", call.getStatus(), "验证Status");
			Logger.verifyEquals(1, count2-count1, "验证未读总数加1");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUnReadCount_003() {
		Logger.start(true,"获取用户与洋管家消息未读数-会话不存在");
		try {
			//客服上线
			service.getOnlineCS(sellerId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(sellerId,userId);
			
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();		
			int count1=Integer.parseInt(call.getResult().get("Count").toString());
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId()+1);//改变消息数
			sendMessageCSBean.setCsId(sellerId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			call.callService();	
			int count2=Integer.parseInt(call.getResult().get("Count").toString());
			
			Logger.verifyEquals("0", call.getResult().get("ErrorCode").toString(), "验证errorCode");
			Logger.verifyEquals(true, call.getResult().get("Success"), "验证Success");
			Logger.verifyEquals("200", call.getStatus(), "验证Status");
			Logger.verifyEquals(0, count2-count1, "验证会话不存在，消息总数不改变");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUnReadCount_004() {
		Logger.start(true,"获取用户与洋管家消息未读数-userId 不存在");
		try {
			bean.setUserId(userId+1111);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("0", call.getResult().get("ErrorCode").toString(), "验证errorCode");
			Logger.verifyEquals(true, call.getResult().get("Success"), "验证Success");
			Logger.verifyEquals("200", call.getStatus(), "验证Status");
			Logger.verifyEquals(0, call.getResult().get("Count"), "验证userId不存在，count=0");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}